//
//  HFWelcomViewController.swift
//  HFWeChat
//
//  Created by 黄来峰 on 16/4/22.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SDWebImage

class HFWelcomViewController: UIViewController {
    
    
    //改变View要在这个方法中执行
    override func loadView() {
        view = welcomeBgImgView
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    }
    //在view已经加载完成以后
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        setHeadImageAnimation()
    }
    
    
    //懒加载控件
    private lazy var welcomeBgImgView: UIImageView = {
        
        let welImgView: UIImageView = UIImageView()
        welImgView.image = UIImage(named: "ad_background")
        
        return welImgView
    }()
    
    //懒加载头像控件
    private lazy var headImgView : UIImageView = {
        let headView = UIImageView()
//        headView.image = UIImage(named: "avatar_default_big")
        headView.layer.cornerRadius = 85/2
        headView.layer.borderColor = ThemeColor.CGColor
        headView.layer.borderWidth = 2

        headView.sd_setImageWithURL(NSURL(string: (HFUserAccountTools.sharedTools.userAccount?.profile_image_url) ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))
        
//         headView.sd_setImageWithURL(NSURL(string: "http://tp1.sinaimg.cn/5890875512/50/5756218044/1"), placeholderImage: UIImage(named: "avatar_default_big"))
        
        
        //下面两句话的功能是一样的
        //        headView.clipsToBounds = true
        headView.layer.masksToBounds = true
        return headView
    }()
    
    //懒加载“欢迎回来”
    private lazy var welcomeBackLabel: UILabel = {
        let welcomeBackLabel = UILabel()
        welcomeBackLabel.textColor = UIColor.darkGrayColor()
        welcomeBackLabel.text = "欢迎回来"
        welcomeBackLabel.font = UIFont.systemFontOfSize(17)
        welcomeBackLabel.alpha = 0
        return welcomeBackLabel
    }()
    
    
    //MARK: -设置UI
    private func setupUI(){
        
        //添加控件
        view.addSubview(headImgView)
        view.addSubview(welcomeBackLabel)
        
        //设置头像约束
        headImgView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 85, height: 85))
            make.centerX.equalTo(view)
            make.centerY.equalTo(view.snp_top).offset(500)
        }
        //设置欢迎回来Label的约束
        welcomeBackLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.centerY.equalTo(headImgView.snp_bottom).offset(15)
        }
        
    }
    //MARK: -设置头像的阻尼动画
    private func setHeadImageAnimation(){
        //更新约束
        headImgView.snp_updateConstraints { (make) -> Void in
            make.centerY.equalTo(view.snp_top).offset(100)
        }
        //设置阻尼动画
        UIView.animateWithDuration(3, delay: 0.6, usingSpringWithDamping: 0.7, initialSpringVelocity: 2, options: [], animations: { () -> Void in
            //让VIew重新布局
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                UIView.animateWithDuration(0.8, animations: { () -> Void in
                    self.welcomeBackLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        
                        //欢迎界面完成之后发通知切换根视图
                        NSNotificationCenter.defaultCenter().postNotificationName(SWITCHDEFAULTVC, object: "welcome")
                        
                })
               

        }
    }
}